﻿package com.tudou.home.model
{
    import com.tudou.events.*;
    import com.tudou.home.commands.*;
    import com.tudou.home.commands.ad.*;
    import com.tudou.home.consts.*;
    import com.tudou.home.model.data.*;
    import com.tudou.home.utils.*;
    import com.tudou.js.*;
    import com.tudou.newPlayer.*;
    import flash.events.*;
    import flash.utils.*;
    import org.puremvc.as3.patterns.proxy.*;

    public class PageProxy extends Proxy
    {
        private var _pageInfo:PageInfo;
        private var _playerStatus:IPlayerStatus;
        private var _savePlayedStart:Boolean;
        private var jsExchanger:JSExchanger;
        private var _savePlayedTimer:Timer;
        private var isPageLoaded:Boolean;
        public static const NAME:String = "PageProxy";
        private static const SAVE_PLAYED_TIME_INTERVAL:Number = 10000;

        public function PageProxy(param1:IPlayerStatus)
        {
            super(NAME);
            _playerStatus = param1;
            _playerStatus.addEventListener(PlayerEvent.CHANGE_VIDEO_STATUS, changeVideoStatusHlr);
            jsExchanger = JSExchanger.getInst();
            _pageInfo = PageInfo.getInstance();
            isPageLoaded = false;
            _savePlayedStart = false;
            _savePlayedTimer = new Timer(SAVE_PLAYED_TIME_INTERVAL);
            _savePlayedTimer.addEventListener(TimerEvent.TIMER, savePlayedTimerHandler);
            addCallBack();
            return;
        }// end function

        public function setRurlId(param1:String) : void
        {
            jsExchanger.justNotifyJS("CPMLoaded", [param1]);
            return;
        }// end function

        public function set pvid(param1:String) : void
        {
            _pageInfo.pvid = param1;
            return;
        }// end function

        public function setBGColor(param1:uint) : void
        {
            Binding.fire(BindingConst.CHANGE_COLOR, [param1]);
            return;
        }// end function

        private function shareLoaded(param1:Object) : void
        {
            var _loc_3:ShareItem = null;
            var _loc_4:Object = null;
            var _loc_2:* = param1 as Array;
            var _loc_5:int = 0;
            while (_loc_5 < _loc_2.length)
            {
                
                _loc_4 = _loc_2[_loc_5];
                _loc_3 = new ShareItem();
                _loc_3._functionName = _loc_4.fnname;
                _loc_3._iconURL = _loc_4.thumbnail;
                _loc_3._siteName = _loc_4.name;
                _loc_2[_loc_5] = _loc_3;
                _loc_5++;
            }
            sendNotification(PageCommandSet.SHARE_DATA_LOADED, _loc_2);
            return;
        }// end function

        public function setLiveData(param1:XML) : void
        {
            var _loc_2:* = param1 != null ? (param1.toXMLString()) : ("");
            jsExchanger.setData("live", _loc_2);
            return;
        }// end function

        public function isPlayerLived() : Boolean
        {
            return true;
        }// end function

        public function loadShareRelativeData() : void
        {
            var judge:Function;
            judge = function (param1:Object) : Boolean
            {
                var _loc_2:* = param1 as Array;
                return _loc_2 && _loc_2.length > 0;
            }// end function
            ;
            jsExchanger.getData("share", shareLoaded, judge);
            jsExchanger.getData("recommend", relativeLoaded, judge);
            return;
        }// end function

        private function playerChangeUrlHlr(event:PlayerEvent) : void
        {
            var judge:Function;
            var pe:* = event;
            judge = function (param1:Boolean) : Boolean
            {
                return param1;
            }// end function
            ;
            jsExchanger.notifyJS("loadVideoUrl", [pe.data], null, judge);
            return;
        }// end function

        public function set cptAd(param1:Object) : void
        {
            sendNotification(AdCommandSet.AD_CPT_FLOAT_LOADED, param1);
            return;
        }// end function

        private function savePlayedTimerHandler(event:Event) : void
        {
            var _loc_2:String = null;
            if (_playerStatus.hasReady)
            {
                _loc_2 = jsExchanger.justNotifyJS("playTimer", [_playerStatus.hasPlayedTime]);
                if (_loc_2 == "false")
                {
                    _savePlayedTimer.stop();
                }
            }
            return;
        }// end function

        public function expand(param1:Boolean) : void
        {
            jsExchanger.notifyJS("wideScreen", [param1]);
            return;
        }// end function

        private function toggleWideScreenHlr(event:JSEvent) : void
        {
            sendNotification(ViewCommandSet.EXPAND_STAGE, event.data[0], NAME);
            return;
        }// end function

        public function checkPageLoaded() : void
        {
            jsExchanger.notifyJS("isPageLoaded", [_pageInfo.soJuid], pageLoaded, function (param1:String) : Boolean
            {
                return param1 == "true";
            }// end function
            );
            return;
        }// end function

        public function startSavePlayedTime() : void
        {
            _savePlayedStart = true;
            if (isPageLoaded)
            {
                _savePlayedTimer.start();
            }
            return;
        }// end function

        public function startPlayVideo(param1:int, param2:Boolean, param3:Object = null) : void
        {
            jsExchanger.justNotifyJS("changeItem", [param1, param2, param3]);
            return;
        }// end function

        private function addCallBack() : void
        {
            jsExchanger.addCallback(this, "isPlayerLived");
            jsExchanger.addCallback(this, "pageLoaded");
            jsExchanger.addCallback(this, "setBGColor");
            jsExchanger.addCallback(this, "playerStatus");
            jsExchanger.shareData(this, "cptAd");
            jsExchanger.shareData(this, "pvid");
            jsExchanger.addEventListener("wideScreen", toggleWideScreenHlr);
            return;
        }// end function

        private function relativeLoaded(param1:Object) : void
        {
            var _loc_4:BaseVideoInfo = null;
            var _loc_5:Object = null;
            var _loc_2:* = param1 as Array;
            var _loc_3:Array = [];
            var _loc_6:int = 0;
            while (_loc_6 < _loc_2.length)
            {
                
                _loc_5 = _loc_2[_loc_6];
                _loc_4 = new BaseVideoInfo();
                _loc_4.duration = _loc_5.time;
                _loc_4.playCount = _loc_5.playNum;
                _loc_4.commentCount = _loc_5.cmtNum;
                _loc_4.frame1URL = _loc_5.picUrl;
                _loc_4.maxRateType = _loc_5.isHd;
                _loc_4.title = _loc_5.title;
                _loc_4.href = _loc_5.url;
                _loc_3[_loc_6] = _loc_4;
                _loc_6++;
            }
            sendNotification(PlayListSingleCommandSet.PLAY_LIST_LOADED, _loc_3);
            return;
        }// end function

        public function pageLoaded(param1:Object = null) : String
        {
            if (isPageLoaded)
            {
                return _pageInfo.soJuid;
            }
            isPageLoaded = true;
            jsExchanger.killNotifyJS("isPageLoaded");
            Binding.fire(BindingConst.PAGE_LOADED);
            if (_savePlayedStart)
            {
                _savePlayedTimer.start();
            }
            sendNotification(PageCommandSet.PAGE_LOADED);
            return _pageInfo.soJuid;
        }// end function

        protected function changeVideoStatusHlr(event:PlayerEvent) : void
        {
            switch(_playerStatus.videoStatus.status)
            {
                case VideoStatus.PLAYING:
                {
                    break;
                }
                case VideoStatus.PAUSED:
                {
                    break;
                }
                case VideoStatus.COMPLETED:
                {
                    break;
                }
                case VideoStatus.BUFFERING:
                {
                    break;
                }
                case VideoStatus.READY:
                {
                    jsExchanger.justNotifyJS("startPlay");
                    break;
                }
                default:
                {
                    break;
                }
            }
            return;
        }// end function

        public function playerStatus() : Object
        {
            var _loc_1:Object = {isPlaying:_playerStatus.isPlaying, playTimePos:_playerStatus.playTimePos};
            return _loc_1;
        }// end function

        public function setAllRateTypeURLs(param1:Array) : void
        {
            var judge:Function;
            var urls:* = param1;
            judge = function (param1:Boolean) : Boolean
            {
                return param1;
            }// end function
            ;
            jsExchanger.notifyJS("v2Loaded", [urls], null, judge);
            return;
        }// end function

    }
}
